package cn.liontalk.controller;

import cn.liontalk.VO.CategoryVO;
import cn.liontalk.VO.ProductVO;
import cn.liontalk.VO.ResultVO;
import cn.liontalk.entity.ProductCategory;
import cn.liontalk.entity.ProductInfo;
import cn.liontalk.service.ProductCategoryService;
import cn.liontalk.service.ProductService;
import cn.liontalk.utils.ResultVoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.awt.geom.AreaOp;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ZhouZhe
 * @version 1.0
 * @description
 * @date 2019-04-15 22:26
 **/
@RestController
@RequestMapping(value = "/product")
public class ProductController {


    @Autowired
    ProductService productService;

    @Autowired
    ProductCategoryService categoryService;


    @GetMapping(value = "/list")
    public ResultVO getAllProductList() {

        List<CategoryVO> voList = new ArrayList<>();
        List<ProductInfo> productInfos = productService.queryByProductStatus(0);

        List<Integer> list = new ArrayList<>();
        for (ProductInfo productInfo : productInfos) {
            list.add(productInfo.getCategoryType());
        }
        List<ProductCategory> categoryVOList = categoryService.queryProductCategoryByCategoryTypeIn(list);
        for (ProductCategory productCategory : categoryVOList) {
            CategoryVO categoryVO = new CategoryVO();
            categoryVO.setCategoryName(productCategory.getCategoryName());
            categoryVO.setCategoryType(productCategory.getCategoryType());
            List<ProductVO> categoryVOS = new ArrayList<>();
            for (ProductInfo productInfo : productInfos) {
                if (productCategory.getCategoryType() == productInfo.getCategoryType()) {
                    ProductVO productVO = new ProductVO();
                    productVO.setProductDescription(productInfo.getProductDescription());
                    productVO.setProductPrice(productInfo.getProductPrice());
                    productVO.setProductIcon(productInfo.getProductIcon());
                    productVO.setProductId(productInfo.getProductId());
                    productVO.setProductName(productInfo.getProductName());
                    categoryVOS.add(productVO);
                }
            }
            categoryVO.setFoods(categoryVOS);
            voList.add(categoryVO);
        }
        return ResultVoUtils.success(voList);
    }


}
